let postInfo = require('../../../data/posts-data.js');
let app = getApp(); 
Page({

  data: {
    postInfo: {},
    postId: '',
    isPlaying : false,
  },

  onLoad: function(options) {
    let id = parseInt(options.id);
    this.setData({
      postInfo: postInfo.postList[id],
      postId: id,
    })

    //1.默认收藏状态
    //前台数据绑定
    let collectedObj = wx.getStorageSync('collected');
    let postId = this.data.postId;
    if (collectedObj) {
      this.setData({
        collected: collectedObj[this.data.postId],
      })
    } else {
      let collectedObj = {};
      collectedObj[postId] = false;
      wx.setStorageSync('collected', collectedObj);
    }

    //全局监听播放状态
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId===postId){
      this.setData({
        isPlaying: true
      })
    }
    this.setMusicMonitor(); //调用监听播放
  },

  //监听播放状态
  setMusicMonitor: function () {
    var that = this;
    wx.onBackgroundAudioPlay(function () {  //播放
      that.setData({
        isPlaying: true
      });
      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_currentMusicPostId = postId;
    });
    wx.onBackgroundAudioPause(function () { //暂停
      that.setData({
        isPlaying: false
      });
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });
    wx.onBackgroundAudioStop(function(){    //停止
      that.setData({
        isPlaying: false
      });
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });
  },

  //点击收藏
  postCollec() {
    //后台数据绑定
    let collectedObj = wx.getStorageSync('collected'); //2.
    let postId = this.data.postId; //3.
    let collected = collectedObj[postId]; //4.
    collected = !collected;
    collectedObj[postId] = collected; //5.
    wx.setStorageSync('collected', collectedObj); //6.
    this.setData({
      collected: collected,
    });
    
    this.showToast(collected);
  },

  //提示框
  showToast(collected){
    wx.showToast({
      title: collected ? '收藏成功' : '取消成功',
      duration: 1000,
    });
  },

  //分享函数
  share(){
    let itemList = ['分享到朋友圈', '分享给好友', '分享到qq'];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: '#405f80',
      success(res) {
        wx.showModal({
          title: itemList[res.tapIndex],
          content: '确定分享?',
        })
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },


  //背景音乐播放
  playing:function(){
    let postId = this.data.postId;
    let postMusic = postInfo.postList[postId].music;
    let isPlaying = this.data.isPlaying;
    if (isPlaying){
      wx.pauseBackgroundAudio()
      this.setData({
        isPlaying : false,
      })
      app.globalData.g_isPlayingMusic = false;
    }else{
      /*
      wx.playBackgroundAudio({
        dataUrl: postMusic.url,
        title: postMusic.title,
        coverImgUrl: postMusic.coverImg
      })
      */
      const BackgroundAudioManager = wx.getBackgroundAudioManager();
      BackgroundAudioManager.src = postMusic.url;
      BackgroundAudioManager.title = postMusic.title;
      console.log(postMusic.url);
      console.log(postMusic.title);
      this.setData({
        isPlaying: true,
      });
      app.globalData.g_currentMusicPostId = postId;
      app.globalData.g_isPlayingMusic = true;
    }
  },
})